home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / comms / internet / html-related / hsc / source / hsclib / lmessage.c < prev    next >
C/C++ Source or Header  |  1996-09-10  |  11KB  |  404 lines

  1. /*
  2.  * hsclib/message.c
  3.  *
  4.  * message functions for hsc
  5.  *
  6.  * Copyright (C) 1995,96  Thomas Aglassinger
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  * updated: 10-Sep-1996
  23.  * created: 10-Mar-1996
  24.  *
  25.  * NOTE: see "hsclib/msgid.h" for message-id's and
  26.  *       how a message-id is build.
  27.  */
  28.  
  29. #define NOEXTERN_HSCLIB_MESSAGE_H
  30.  
  31. #include "hsclib/inc_base.h"
  32.  
  33. static VOID msg_tag(EXPSTR * msgstr, CONSTRPTR tagname)
  34. {
  35.     app_estr(msgstr, "tag <");
  36.     app_estr(msgstr, tagname);
  37.     app_estr(msgstr, ">");
  38. }
  39.  
  40. static VOID msg_endtag(EXPSTR * msgstr, CONSTRPTR tagname)
  41. {
  42.     app_estr(msgstr, "end-tag </");
  43.     app_estr(msgstr, tagname);
  44.     app_estr(msgstr, ">");
  45. }
  46.  
  47. static VOID msg_attr(EXPSTR * msgstr, CONSTRPTR attrname)
  48. {
  49.     app_estr(msgstr, "attribute ");
  50.     app_estr(msgstr, attrname);
  51. }
  52.  
  53. static VOID msg_entity(EXPSTR * msgstr, CONSTRPTR entname)
  54. {
  55.     app_estr(msgstr, "entity `");
  56.     app_estr(msgstr, entname);
  57.     app_estr(msgstr, "'");
  58. }
  59.  
  60. static VOID msg_idname(EXPSTR * msgstr, CONSTRPTR idname)
  61. {
  62.     app_estr(msgstr, "id ");
  63.     app_estr(msgstr, "\"#");
  64.     app_estr(msgstr, idname);
  65.     app_estrch(msgstr, '"');
  66. }
  67.  
  68. /*
  69.  * hsc_message
  70.  *
  71.  * create message string and send it to call-back
  72.  *
  73.  * legal placeholders inside format:
  74.  *  %A ptr to HSCATTR
  75.  *  %a string for attribute-name
  76.  *  %d dezimal number (LONG)
  77.  *  %E ptr to HSCENT
  78.  *  %e string to entity-name
  79.  *  %i string to id-name
  80.  *  %q quoted string
  81.  *  %s string
  82.  *  %T ptr to HSCTAG
  83.  *  %t string for tag-name
  84.  *
  85.  * example:
  86.  * ---
  87.  *  HSCTAG *mytag;
  88.  *  STRPTR expected_tag = "dummy";
  89.  *
  90.  *  hsc_message( hp, MSG_my_tag_expected,
  91.  *               "Expected tag %T insted of %t",
  92.  *               mytag, expected_tag );
  93.  * ---
  94.  */
  95. VOID hsc_message(HSCPRC * hp, HSCMSG_ID msg_id, const char *format,...)
  96. {
  97.     HSCMSG_CLASS msg_class = msg_id & MASK_MSG_CLASS;
  98.     INFILE *msg_inpf = NULL;
  99.     STRPTR msg_fname = "unknown";
  100.     ULONG msg_x = 0;
  101.     ULONG msg_y = 0;
  102.     BOOL disp_msg = TRUE;       /* flag, if message really */
  103.     /* should be displayed */
  104.     if (hp->fatal)
  105.     {
  106.  
  107.         /* oppress all messages after fatal errors */
  108.         disp_msg = FALSE;
  109.  
  110.     }
  111.     else if (
  112.                 (hsc_get_msg_ignore(hp, msg_id))
  113.                 &&
  114.                 (hsc_get_msg_class(hp, msg_id) <= MSG_WARN)
  115.         )
  116.     {
  117.         /* oppress message if it is marked as ignored
  118.          * and it is no ERROR/FATAL message
  119.          */
  120.         disp_msg = FALSE;
  121.     }
  122.  
  123.     if (disp_msg)
  124.     {
  125.         va_list ap;
  126.  
  127.         /* increase message-counter */
  128.         hp->msg_count++;
  129.  
  130.         /* set fatal-flag, if this is a fatal message */
  131.         if (msg_id > MSG_FATAL)
  132.             hp->fatal = TRUE;
  133.  
  134.         /* clear message buffer */
  135.         clr_estr(hp->curr_msg);
  136.  
  137.         /* create message string */
  138.         va_start(ap, format);
  139.         while (format[0])
  140.         {
  141.             if (format[0] == '%')
  142.             {
  143.                 STRPTR s = NULL;
  144.                 HSCTAG *tag = NULL;
  145.                 HSCATTR *attr = NULL;
  146.                 HSCENT *ent = NULL;
  147.  
  148.                 format++;
  149.                 switch (format[0])
  150.                 {
  151.  
  152.                 case 'd':
  153.                     /*
  154.                      * append decimal number
  155.                      */
  156.                     app_estr(hp->curr_msg,
  157.                              long2str(va_arg(ap, LONG)));
  158.                     break;
  159.  
  160.                 case 'q':
  161.                     /*
  162.                      * append quoted string
  163.                      */
  164.                     s = va_arg(ap, STRPTR);
  165.  
  166.                     app_estrch(hp->curr_msg, '`');
  167.                     while (s[0])
  168.                     {
  169.                         switch (s[0])
  170.                         {
  171.  
  172.                         case '\n':
  173.                             app_estr(hp->curr_msg, "\\n");
  174.                             break;
  175.                         case '\"':
  176.                             app_estr(hp->curr_msg, "\\\"");
  177.                             break;
  178.                         default:
  179.                             if (s[0] < ' ')
  180.                             {
  181.                                 app_estrch(hp->curr_msg, '\\');
  182.                                 app_estr(hp->curr_msg,
  183.                                          long2str((LONG) s[0]));
  184.                                 app_estrch(hp->curr_msg, ';');
  185.                             }
  186.                             else
  187.                                 app_estrch(hp->curr_msg, s[0]);
  188.                         }
  189.                         s++;    /* process next char */
  190.                     }
  191.                     app_estrch(hp->curr_msg, '\'');
  192.  
  193.                     break;
  194.  
  195.                 case 's':
  196.                     /*
  197.                      * append simple string
  198.                      */
  199.                     app_estr(hp->curr_msg, va_arg(ap, STRPTR));
  200.                     break;
  201.  
  202.                 case 'T':
  203.                     /* append tag-pointer */
  204.                     tag = va_arg(ap, HSCTAG *);
  205.                     msg_tag(hp->curr_msg, tag->name);
  206.                     break;
  207.  
  208.                 case 't':
  209.                     /* append tag */
  210.                     msg_tag(hp->curr_msg, va_arg(ap, STRPTR));
  211.                     break;
  212.  
  213.                 case 'C':
  214.                     /* append end tag-pointer */
  215.                     tag = va_arg(ap, HSCTAG *);
  216.                     msg_endtag(hp->curr_msg, tag->name);
  217.                     break;
  218.  
  219.                 case 'c':
  220.                     /* append end tag */
  221.                     msg_endtag(hp->curr_msg, va_arg(ap, STRPTR));
  222.                     break;
  223.  
  224.                 case 'A':
  225.                     /* append attribute-pointer */
  226.                     attr = va_arg(ap, HSCATTR *);
  227.                     msg_attr(hp->curr_msg, attr->name);
  228.                     break;
  229.  
  230.                 case 'a':
  231.                     /* append attribute */
  232.                     msg_attr(hp->curr_msg, va_arg(ap, STRPTR));
  233.                     break;
  234.  
  235.                 case 'E':
  236.                     /* append entity-pointer */
  237.                     ent = va_arg(ap, HSCENT *);
  238.                     msg_entity(hp->curr_msg, ent->name);
  239.                     break;
  240.  
  241.                 case 'e':
  242.                     /* append entity */
  243.                     msg_entity(hp->curr_msg, va_arg(ap, STRPTR));
  244.                     break;
  245.  
  246.                 case 'i':
  247.                     /* append ID */
  248.                     msg_idname(hp->curr_msg, va_arg(ap, STRPTR));
  249.                     break;
  250.  
  251.                 default:
  252.                     /*
  253.                      * append unknown
  254.                      */
  255.                     app_estrch(hp->curr_msg, '%');
  256.                     if (format[0] && (format[0] != '%'))
  257.                     {
  258.                         app_estrch(hp->curr_msg, '%');
  259.                         format--;
  260.                     }
  261.                     break;
  262.                 }
  263.             }
  264.             else
  265.                 app_estrch(hp->curr_msg, format[0]);
  266.  
  267.             if (format[0])
  268.                 format++;
  269.         }
  270.         va_end(format);
  271.  
  272.         /* evaluate message position */
  273.         if (hp->inpf)
  274.         {
  275.             msg_inpf = hp->inpf;
  276.             msg_fname = infget_fname(msg_inpf);
  277.  
  278.             /* is parent file for position? */
  279.             if (!strncmp(msg_fname, PARENT_FILE_ID,
  280.                          strlen(PARENT_FILE_ID)))
  281.             {
  282.                 /* use position of first file on stack */
  283.                 msg_inpf = (INFILE *) dln_data(dll_first(hp->inpf_stack));
  284.                 msg_fname = infget_fname(msg_inpf);
  285.                 D(fprintf(stderr, DHL "msg from spec.file\n"));
  286.             }
  287.             msg_x = infget_wx(msg_inpf) + 1;
  288.             msg_y = infget_wy(msg_inpf) + 1;
  289.         }
  290.         else
  291.         {
  292.             msg_fname = NULL;
  293.             msg_x = 0;
  294.             msg_y = 0;
  295.         }
  296.  
  297.         /* send message via callback */
  298.         if (hp->CB_message)
  299.  
  300.             (*(hp->CB_message))
  301.                 (hp,
  302.                  msg_class,
  303.                  msg_id & MASK_MESSAGE,
  304.                  msg_fname, msg_x, msg_y,
  305.                  estr2str(hp->curr_msg)
  306.                 );
  307.  
  308.         /* process nested files */
  309.         if (hp->CB_message_ref)
  310.         {
  311.             DLNODE *nd = dll_first(hp->inpf_stack);
  312.  
  313.             while (nd)
  314.             {
  315.                 msg_inpf = dln_data(nd);
  316.                 msg_fname = infget_fname(msg_inpf);
  317.                 msg_x = infget_wx(msg_inpf) + 1;
  318.                 msg_y = infget_wy(msg_inpf) + 1;
  319.  
  320.                 (*(hp->CB_message_ref))
  321.                     (hp,
  322.                      msg_class,
  323.                      msg_id & MASK_MESSAGE,
  324.                      msg_fname, msg_x, msg_y,
  325.                      ""
  326.                     );
  327.  
  328.                 nd = dln_next(nd);
  329.             }
  330.         }
  331.     }
  332.     else
  333.     {
  334.         D(fprintf(stderr, DHL "suppressed msg#%ld\n", msg_id & MASK_MESSAGE));
  335.     }
  336. }
  337.  
  338. /*
  339.  *-------------------------------------
  340.  * often occurable errors & messages
  341.  *-------------------------------------
  342.  */
  343.  
  344. VOID hsc_msg_eof(HSCPRC * hp, STRPTR descr)
  345. {
  346.     STRPTR eoftxt = "unexpected end of file";
  347.  
  348.     if (descr)
  349.         hsc_message(hp, MSG_UNEX_EOF, "%s (%s)", eoftxt, descr);
  350.     else
  351.         hsc_message(hp, MSG_UNEX_EOF, "%s", eoftxt);
  352. }
  353.  
  354. VOID hsc_msg_illg_whtspc(HSCPRC * hp)
  355. {
  356.     hsc_message(hp, MSG_UNEX_EOF, "illegal white-space");
  357. }
  358.  
  359. VOID hsc_msg_stripped_tag(HSCPRC * hp, HSCTAG * tag, STRPTR why)
  360. {
  361.     if (why)
  362.         hsc_message(hp, MSG_TAG_STRIPPED,
  363.                     "stripped tag %T (%s)", tag, why);
  364.     else
  365.         hsc_message(hp, MSG_TAG_STRIPPED,
  366.                     "stripped tag %T", tag);
  367. }
  368.  
  369. VOID hsc_msg_unkn_attr(HSCPRC * hp, STRPTR attr)
  370. {
  371.     hsc_message(hp, MSG_UNKN_ATTR,
  372.                 "unknown %a", attr);
  373. }
  374.  
  375. VOID hsc_msg_eol(HSCPRC * hp)
  376. {
  377.     hsc_message(hp, MSG_UNEX_EOL,
  378.                 "unexpected end of line");
  379. }
  380.  
  381. VOID hsc_msg_noinput(HSCPRC * hp, STRPTR filename)
  382. {
  383.     hsc_message(hp, MSG_NO_INPUT,
  384.                 "can not open %q for input: %s",
  385.                 filename, strerror(errno));
  386. }
  387.  
  388. VOID hsc_msg_nouri(HSCPRC * hp, STRPTR filename, STRPTR uriname, STRPTR note)
  389. {
  390.     if (note)
  391.     {
  392.         hsc_message(hp, MSG_NO_URIPATH,
  393.                     "file %q for URI %q not found (%s)",
  394.                     filename, uriname, note);
  395.     }
  396.     else
  397.     {
  398.         hsc_message(hp, MSG_NO_URIPATH,
  399.                     "file %q for URI %q not found",
  400.                     filename, uriname);
  401.     }
  402. }
  403.  
  404.